package Tools;

import java.util.ArrayList;
import java.util.List;

import com.mobilelmes.TodoListItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class TodoProvider {
	private static final String DB_NAME = "tasks";
	private static final String TABLE_NAME = "tasks";
	private static final int DB_VERSION = 1;
	private static final String DB_CREATE_QUERY = "CREATE TABLE " + TABLE_NAME
			+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL);";

	private SQLiteDatabase storage;
	private SQLiteOpenHelper helper;
	ArrayList<TodoListItem> item = new ArrayList<TodoListItem>();
	public TodoProvider(Context ctx) {
		Log.d("Allen",DB_CREATE_QUERY);
		helper = new SQLiteOpenHelper(ctx, DB_NAME, null, DB_VERSION) {
			@Override
			public void onUpgrade(SQLiteDatabase db, int oldVersion,
					int newVersion) {
				db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
				onCreate(db);
			}

			@Override
			public void onCreate(SQLiteDatabase db) {
				db.execSQL(DB_CREATE_QUERY);
				Log.d("Allen",DB_CREATE_QUERY);
				if(null!=db)
				{
					Log.d("Allen","NOT NULL");
				}
			}
		};
		storage = helper.getWritableDatabase();
		if(null==storage){
			Log.d("Allen", "NOOT NULLLL 45");
		}
	}


	public List<String> findAll() {
		List<String> tasks = new ArrayList<String>();
		item.clear();
		Cursor c = storage.query(TABLE_NAME, new String[] { "id" , "title" }, null,
				null, null, null, null);
		if (c != null) {
			c.moveToFirst();
			while (c.isAfterLast() == false) {
				TodoListItem i = new TodoListItem(c.getInt(0),c.getString(1));
				item.add(i);
				tasks.add(c.getString(1));
				c.moveToNext();
			}
			c.close();
		}
		return tasks;
	}

	public void addTask(String title) {
		if (!title.isEmpty())
		{			
			ContentValues data = new ContentValues();
			data.put("title", title);
			storage.insert(TABLE_NAME, null, data);
			findAll();
			Log.d("Lo_addTask","Added");
		}
		else
			Log.d("Lo_addTask","Not Added");
	}

	public void deleteTask(String title) {
		storage.delete(TABLE_NAME, "title='" + title + "'", null);
		//String selectQuery = " select * from " + TABLE_NAME + " where id = " + id + " and title ='" + title + "'";
		//storage.rawQuery(selectQuery, null);
	}

	public void deleteTask(int id, String title) {
		String selectQuery = "delete from " + TABLE_NAME + " where id = " + id + " and title ='" + title + "'";
		storage.rawQuery(selectQuery, null);
	}
	
	public void deleteTask(int row) {
		Log.d("Allen","ROW : " + String.valueOf(row));
		int tmp = row-1;
		TodoListItem i = item.get(tmp);
		Log.d("Allen","ID : " + String.valueOf(i.getID()));
//		String.valueOf(i.getID())
		//String deleteQuery = "delete from " + TABLE_NAME + " where id = " + i.getID();
		//Log.d("deleteTask",deleteQuery);
		storage.delete(TABLE_NAME, "id = " + String.valueOf(i.getID()),null);
		//storage.rawQuery(deleteQuery, null);
		item.remove(tmp);
		for(TodoListItem a:item){
			Log.d("allen", String.valueOf(a.getID()));
		}
//		String n = "";
//		String selectQuery = "select id from " + TABLE_NAME + " a where  (select count(*) from tasks b  where a.id >= b.id) = " + id;
//		Log.d("deleteTask",selectQuery);
//		Cursor c = storage.rawQuery(selectQuery, null);
//		if (c != null) {
//			c.moveToFirst();
//			while (c.isAfterLast() == false) {
//				n = c.getString(0);	
//				c.moveToNext();
//			}
//			c.close();
//		}
//		
//		String selectQuery2 = "delete from " + TABLE_NAME + " where id = " + n;
//		Log.d("deleteTask",selectQuery2);
//		storage.rawQuery(selectQuery2, null);
	}

}

